package com.art.sunflower.controller;

import com.art.sunflower.exception.NullNewsException;
import com.art.sunflower.pojo.SchoolTeacher;
import com.art.sunflower.service.SchoolTeacherService;
import com.art.sunflower.util.AddFile;
import com.art.sunflower.util.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping(value = "")
@Api(value = "老师控制器")
public class SchoolTeacherController {
    @Resource
    private SchoolTeacherService schoolTeacherService;


    @PostMapping("SchoolTeacher")
    @RequiresRoles(value = {"admin"})
    @ApiOperation("增加一条教师信息")
    public Message insert(@RequestParam(value = "file", required = false) MultipartFile file, String name, String information) {
        AddFile addFile = new AddFile();
        String url = addFile.addFile(file, "schoolTeacher");
        SchoolTeacher schoolTeacher = new SchoolTeacher();
        schoolTeacher.setUrl(url);
        schoolTeacher.setName(name);
        schoolTeacher.setIsIndex(false);
        schoolTeacher.setInformation(information);
        System.out.println(schoolTeacher.toString());
        int count = schoolTeacherService.insert(schoolTeacher);
        if (count > 0) {
            return Message.createSuc("添加成功");
        }
        return Message.createErr("增加失败");
    }


    @DeleteMapping("SchoolTeacher/{id}")
    @RequiresRoles(value = {"admin"})
    @ApiOperation("删除一条教师")
    public Message delete(@RequestParam("id") int id) {
        int count = schoolTeacherService.delete(id);
        if (count > 0) {
            return Message.createSuc(count);
        }
        return Message.createErr("删除失败");
    }


    @GetMapping("SchoolTeacher")
    @ApiOperation("分页获取首页教师的列表")
    public Message getList(Integer pageSize, Integer pageNo) {
        try {
            return Message.createSuc(schoolTeacherService.listAll(pageSize, pageNo));
        } catch (NullNewsException e) {
            e.printStackTrace();
            return Message.createErr("教师不存在");
        }

    }

    @GetMapping("SchoolTeacher/{id}")
    @ApiOperation("获取一个首页教师信息")
    public Message selectById(@RequestParam("id") int id) {
        if (schoolTeacherService.selectOne(id) == null) {
            return Message.createErr("编号不存在，查找失败");
        } else {
            return Message.createSuc(schoolTeacherService.selectOne(id));
        }
    }

    @PutMapping("SchoolTeacher")
    @RequiresRoles(value = {"admin"})
    @ApiOperation("修改一个首页教师信息")
    public Message update(@RequestParam(value = "file", required = false) MultipartFile file, int id, String name, String information) {
        SchoolTeacher schoolTeacher = schoolTeacherService.selectOne(id);
        if (schoolTeacher != null) {
            schoolTeacher.setName(name);
            schoolTeacher.setInformation(information);
            if (file != null) {
                AddFile addFile = new AddFile();
                schoolTeacher.setUrl(addFile.addFile(file, "SchoolTeacher"));
            }
        }
        int count = schoolTeacherService.update(schoolTeacher);
        if (count > 0) {
            return Message.createSuc("修改成功");
        }
        return Message.createErr("修改失败");
    }

    @PutMapping("SchoolTeacher/{id}")
    @RequiresRoles(value = {"admin"})
    @ApiOperation("是否上传到首页")
    public Message updateIndex(int id) {
        SchoolTeacher schoolTeacher = schoolTeacherService.selectOne(id);
        if (schoolTeacher != null && schoolTeacher.getIsIndex() != true) {
            schoolTeacher.setIsIndex(Boolean.TRUE);
            schoolTeacherService.updateIndex(id, schoolTeacher.getIsIndex());
            return Message.createSuc("首页展示");
        } else {
            schoolTeacher.setIsIndex(Boolean.FALSE);
            schoolTeacherService.updateIndex(id, schoolTeacher.getIsIndex());
            return Message.createSuc("取消首页展示");
        }
    }
}


